// pages/mine/invoice/list.js
const App = getApp()
import Kits from '../../../utils/kits'

Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    state: {
      type: String,
      default: '0'
    }
  },
  data: {
    init: false,
    loading: false,
    triggered: true,
    CustomBar: App.Global.CustomBar,
    page: 1,
    size: 8,
    time: new Date().getTime(),
    pager: {
      now: 1,
      row: 0,
      all: 0,
      fms: 0,
    },
    items: []
  },

  lifetimes: {
    attached() {

      this.execLoad(false)
    },
    detached() {

    },
  },
  methods: {
    doPulldown() {
      this.data.page = 1
      this.execLoad(true)
    },
    doReachBottom() {
      if (this.data.page >= this.data.pager.all) {
        return
      }
      this.data.page = this.data.page + 1
      this.execLoad(true)
    },

    execLoad(pulldown) {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/wallet/invoice/list',
        body: {
          pager: {
            page: this.data.page,
            size: this.data.size,
            time: this.data.time
          },
          query: {
            begin: '2022-01-01',
            close: '2099-12-31',
            state: parseInt(this.data.state)
          }
        },
        fnSucc: resp => {
          if (resp.data.pager.now == 1) {
            this.data.time = resp.data.pager.fms
          }
          resp.data.items.forEach(item => {
            item.amountStr = Kits.fmtMoneyString(item.amount)
          })

          this.setData({
            init: true,
            loading: false,
            triggered: false,
            pager: resp.data.pager,
            items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
          })
        },
        fnComp: () => {
          if (pulldown) {
            this.triggerEvent('stopPullDown', {})
          }
        }
      })
    },
    // 去详情
    toPage(e) {
      wx.navigateTo({
        url: e.currentTarget.dataset.uri
      })
    },
  }
})